package com.southwind.controller;

import com.southwind.common.DataGridView;
import com.southwind.entity.AdEffect;
import com.southwind.service.AdEffectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/echarts")
public class AdEffectChartController {

    @Autowired
    private AdEffectService trainPlanService;
    @GetMapping("/loadAdEffectData")
    public DataGridView loadAdEffectData() {
        List<AdEffect> list = trainPlanService.list();

        // 用于合并同名广告的统计数据
        Map<String, Map<String, Object>> grouped = new HashMap<>();

        for (AdEffect ad : list) {
            String name = ad.getAdName();

            // 初始化默认值
            if (!grouped.containsKey(name)) {
                Map<String, Object> map = new HashMap<>();
                map.put("pcount", 0);
                map.put("dcount", 0);
                map.put("zcount", 0);
                grouped.put(name, map);
            }

            // 累加
            Map<String, Object> stats = grouped.get(name);
            stats.put("pcount", (int) stats.get("pcount") + (ad.getPcount() != null ? ad.getPcount() : 0));
            stats.put("dcount", (int) stats.get("dcount") + (ad.getDcount() != null ? ad.getDcount() : 0));
            stats.put("zcount", (int) stats.get("zcount") + (ad.getZcount() != null ? ad.getZcount() : 0));
        }

        // 构造最终响应结构
        List<String> adNames = new ArrayList<>();
        List<Integer> pcount = new ArrayList<>();
        List<Integer> dcount = new ArrayList<>();
        List<Integer> zcount = new ArrayList<>();
        List<Float> ctr = new ArrayList<>();

        for (Map.Entry<String, Map<String, Object>> entry : grouped.entrySet()) {
            String name = entry.getKey();
            Map<String, Object> stat = entry.getValue();

            int p = (int) stat.get("pcount");
            int d = (int) stat.get("dcount");
            int z = (int) stat.get("zcount");

            adNames.add(name);
            pcount.add(p);
            dcount.add(d);
            zcount.add(z);

            // 计算点击率
            if (p != 0) {
                ctr.add((float) d * 100 / p);
            } else {
                ctr.add(0f);
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("adNames", adNames);
        result.put("pcount", pcount);
        result.put("dcount", dcount);
        result.put("zcount", zcount);
        result.put("ctr", ctr);

        return new DataGridView(result);
    }
    @GetMapping("/loadPlatformEffectData")
    public DataGridView loadPlatformEffectData() {
        List<AdEffect> list = trainPlanService.list(); // 查询全部广告记录

        // Map<平台名, 聚合数据>
        Map<String, Map<String, Integer>> grouped = new HashMap<>();

        for (AdEffect ad : list) {
            String platform = ad.getAdPlatform();
            if (platform == null) continue;

            grouped.putIfAbsent(platform, new HashMap<>());
            Map<String, Integer> stats = grouped.get(platform);

            stats.put("pcount", stats.getOrDefault("pcount", 0) + (ad.getPcount() != null ? ad.getPcount() : 0));
            stats.put("dcount", stats.getOrDefault("dcount", 0) + (ad.getDcount() != null ? ad.getDcount() : 0));
            stats.put("zcount", stats.getOrDefault("zcount", 0) + (ad.getZcount() != null ? ad.getZcount() : 0));
        }

        // 构造返回数据
        List<String> platforms = new ArrayList<>();
        List<Integer> pcounts = new ArrayList<>();
        List<Integer> dcounts = new ArrayList<>();
        List<Integer> zcounts = new ArrayList<>();

        for (Map.Entry<String, Map<String, Integer>> entry : grouped.entrySet()) {
            String platform = entry.getKey();
            Map<String, Integer> stat = entry.getValue();

            platforms.add(platform);
            pcounts.add(stat.getOrDefault("pcount", 0));
            dcounts.add(stat.getOrDefault("dcount", 0));
            zcounts.add(stat.getOrDefault("zcount", 0));
        }

        Map<String, Object> result = new HashMap<>();
        result.put("platforms", platforms);
        result.put("pcount", pcounts);
        result.put("dcount", dcounts);
        result.put("zcount", zcounts);

        return new DataGridView(result);
    }


    @GetMapping("/loadPlatformPieData")
    public DataGridView loadPlatformPieData() {
        List<AdEffect> list = trainPlanService.list();

        // Map<平台名称, 曝光量累计>
        Map<String, Integer> platformMap = new HashMap<>();

        for (AdEffect ad : list) {
            String platform = ad.getAdPlatform();
            if (platform == null) continue;

            int pcount = ad.getPcount() != null ? ad.getPcount() : 0;
            platformMap.put(platform, platformMap.getOrDefault(platform, 0) + pcount);
        }

        // 构建饼图数据格式
        List<Map<String, Object>> pieData = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : platformMap.entrySet()) {
            Map<String, Object> item = new HashMap<>();
            item.put("name", entry.getKey());
            item.put("value", entry.getValue());
            pieData.add(item);
        }

        return new DataGridView(pieData);
    }

}
